package cn.jslancer.spider;

import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author jslancer
 * @date 2017/11/5
 */
public class UrlDownloader implements Downloader {
    @Override
    public String download(String url) throws IOException {
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        URL pageUrl = new URL(url);
        URLConnection urlConnection = pageUrl.openConnection();
        urlConnection.connect();
        InputStream inputStream = urlConnection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }
}
